package com.tzxs.pay.main.deposit.viewmodel;

import android.app.Application;
import android.arch.lifecycle.AndroidViewModel;
import android.databinding.ObservableArrayList;
import android.support.annotation.NonNull;
import android.support.design.widget.Snackbar;
import android.util.Log;

import com.tzxs.pay.main.bill.model.AutBillImp;
import com.tzxs.pay.main.bill.model.AutOrderBean;
import com.tzxs.pay.main.bill.source.BillDataSource;
import com.tzxs.pay.util.SingleLiveEvent;
import com.tzxs.pay.util.netnavigator.SimpleNetNavigator;
import com.tzxs.pay.util.viewmodel.Callback;

//押金统计
public class DepositViewModel extends AndroidViewModel {

    private static final String TAG = DepositViewModel.class.getSimpleName();
    //提示
    public SingleLiveEvent<String> snackBarText = new SingleLiveEvent<>();
    public SingleLiveEvent<String> dialogText = new SingleLiveEvent<>();

    public ObservableArrayList<AutBillImp> depositList = new ObservableArrayList<>();


    public DepositViewModel(@NonNull Application application) {
        super(application);
    }


    //获取押金统计列表
    public void getDepositBills(String type, int page, int pageNum, String outOrderNo,
                                String remarks, String timeStart, String timeEnd, int payStatus, String typeTime, Callback callback) {
        BillDataSource.getINSTANCE(getApplication()).getDepositBills(type,
                page, pageNum, outOrderNo, remarks, timeStart, timeEnd, payStatus, typeTime, new SimpleNetNavigator<AutOrderBean>(snackBarText, dialogText, callback) {
                    @Override
                    public String getTitle() {
                        return null;
                    }

                    @Override
                    public void success(AutOrderBean data) {
                        super.success(data);
                        //将所有的列表添加到集合
                        if (data.getCode() == 10001) {
                            if (page == 1) {
                                if (depositList != null) {
                                    depositList.clear();
                                }
                                depositList.add(data.getX_data());
                                depositList.addAll(data.getData());
                            } else {
                                depositList.addAll(data.getData());
                            }
                            Log.d(TAG, "success: 押金统计size:" + data.getData().size());
                            callback.success();
                        } else if (data.getCode() == 10002) {
                            if (page == 1) {
                                depositList.clear();
                            }
                            snackBarText.setValue("未查询到数据!");
                        }


                    }

                    @Override
                    public void fail(String error) {

                        Log.d(TAG, "fail: 获取数据失败：" + error);
                    }

                });
    }


}
